$(document).ready(function()
{
	$('#basicForm').submit(function(){
		query = "1d20";
		if(document.getElementById('basicQ').value != "")
		{
			modifiers = document.getElementById('basicQ').value;
			if(modifiers.charAt(0) != '-' && modifiers.charAt(0) != '+')
			{
				modifiers = '+' + modifiers;
			}
			query += modifiers;
		}
		makeCall(query);
		return false;
	});
	$('#advancedForm').submit(function(){
		makeCall(document.getElementById('advancedQ').value);
		return false;
	});
	});

function makeCall(query)
{
	// remove results from any previous run...
	if(document.getElementById("results"))
	{
		$('#results').remove();
	}
	$('#intro').after('<div id="results"></div>');
	
	$.get("roll.php", {q: query}, function(data){
			var roll = data.getElementsByTagName("roll");
			for(i=0; i<roll.length; ++i)
			{
				var total = roll[i].getElementsByTagName("total")[0].firstChild.nodeValue;
				var req = roll[i].getElementsByTagName("want")[0].firstChild.nodeValue;
				var raw = roll[i].getElementsByTagName("raw")[0].firstChild.nodeValue;
				var mod = roll[i].getElementsByTagName("modifier")[0].firstChild.nodeValue;
				$('#results').append('<dl class="result"><dt class="req">Requested</dt><dd>'+req+'</dd><dt>Raw</dt><dd>'+raw+'</dd><dt>Modifier</dt><dd>'+mod+'</dd><dt class="total">Total</dt><dd>'+total+'</dd></dl>');
			}
	});
}